﻿using Tetris.Game.Common.TetrisCenter.TetrisBase;
using Tetris.Game.ControlsBase;

namespace Tetris.Game.Common.Algorithm
{
    public class PierreDellacherie : IAlgorithm
    {
        private readonly BlockControl[,] _blockControls;
        private readonly int _rows = 0;
        private readonly int _cols = 0;
        public PierreDellacherie(BlockControl[,] blocks, int rows, int cols)
        {
            _blockControls = blocks;
            _rows = rows;
            _cols = cols;
        }
        public double A1 { get; set; } = -4.500158825082766d;
        public double A2 { get; set; } = 3.4181268101392694d;
        public double A3 { get; set; } = -3.2178882868487753d;
        public double A4 { get; set; } = -9.348695305445199d;
        public double A5 { get; set; } = -7.899265427351652d;
        public double A6 { get; set; } = -3.3855972247263626d;
        private double EvaluateGameState()
        {
            return A1 * 0d;
        }

        /// <summary>
        /// 指当前板块放置之后，板块重心距离游戏区域底部的距离（也就是小方块的海拔高度）,相当于高度越低越安全
        /// </summary>
        /// <param name="blockShapeBase"></param>
        /// <returns></returns>
        private int GetLandingHeight(BlockShapeBase blockShapeBase)
        {
            return _rows - blockShapeBase.Center.Y;
        }
        /// <summary>
        /// 这是消除参数的体现，他代表的是消除的行数与当前摆放的板块中被消除的小方块的格数的乘积。 
        /// 举个例子：下面这个例子就是说明红色的小方块下落之后会消除2行，而且自身贡献的小方格数是3个，所以返回值是3*2=6 
        /// </summary>
        /// <param name="blockShapeBase"></param>
        /// <returns></returns>
        private int GetErodedPieceCellsMetric(BlockShapeBase blockShapeBase)
        {
            int i, j;
            int erodedPieceCellsMetric = 0;
            int minHeight = 0, maxHeight = 0, rowContribution = 0, fullRow = 0;
            bool isFull = false;
            foreach (var item in blockShapeBase.BlockControls)
            {
                if (minHeight > item.PositionY)
                    minHeight = item.PositionY;
                if (maxHeight < item.PositionY)
                    maxHeight = item.PositionY;
            }
            for (i = minHeight; i <= maxHeight; i++)
            {
                rowContribution = 0;
                isFull = false;
                for (j = 0; j < _cols; j++)
                {
                    BlockControl block = _blockControls[i, j];
                    if (block == null)
                    {
                        isFull = false;
                        break;
                    }
                    // 检查块是否为下落形状的一部分
                    if (blockShapeBase.BlockControls.Contains(block))
                    {
                        rowContribution++;
                        isFull = true;
                    }
                }
                if (isFull)
                {
                    fullRow++;
                    erodedPieceCellsMetric += rowContribution;
                }
            }

            return erodedPieceCellsMetric * fullRow;
        }


    }
}
